<template>
  <div class="friend-requests-page">
    <div class="page-header">
      <div class="header-left">
        <el-button type="text" @click="goBack">
          <el-icon><ArrowLeft /></el-icon>
          返回
        </el-button>
        <h2>好友请求</h2>
      </div>
      <el-button type="primary" size="small" @click="fetchFriendRequests" :loading="requestLoading">
        刷新
      </el-button>
    </div>

    <div v-if="friendRequests.length === 0" class="empty-requests">
      <el-empty description="暂无好友请求" />
    </div>

    <div v-else class="request-list">
      <div v-for="request in friendRequests" :key="request.id" class="request-item">
        <el-avatar :size="60" :src="request.avatar" />
        <div class="request-info">
          <div class="request-name">
            {{ request.nickname }}
            <span class="request-tfid">TF ID: {{ request.tfId }}</span>
          </div>
          <div class="request-email">{{ request.email }}</div>
          <div class="request-time">{{ formatTime(request.createTime) }}</div>
        </div>
        <div class="request-actions">
          <el-button type="success" size="small" @click="handleRequestAction(request, 'accept')">
            接受
          </el-button>
          <el-button type="danger" size="small" @click="handleRequestAction(request, 'reject')">
            拒绝
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { ArrowLeft } from '@element-plus/icons-vue'
import { getFriendRequests, handleFriendRequest } from '@/api/friend'

export default {
  name: 'FriendRequests',
  components: {
    ArrowLeft
  },
  setup() {
    const router = useRouter()
    const friendRequests = ref([])
    const requestLoading = ref(false)

    // 返回上一页
    const goBack = () => {
      router.back()
    }

    // 获取好友请求列表
    const fetchFriendRequests = () => {
      requestLoading.value = true
      getFriendRequests()
        .then(res => {
          console.log('好友请求响应:', res)
          friendRequests.value = res || []
        })
        .catch(error => {
          console.error('获取好友请求失败:', error)
          ElMessage.error('获取好友请求失败')
          friendRequests.value = []
        })
        .finally(() => {
          requestLoading.value = false
        })
    }

    // 处理好友请求
    const handleRequestAction = (request, action) => {
      handleFriendRequest(request.id, action)
        .then(() => {
          ElMessage.success(action === 'accept' ? '已接受好友请求' : '已拒绝好友请求')
          // 重新获取好友请求列表
          fetchFriendRequests()
        })
        .catch(error => {
          console.error('处理好友请求失败:', error)
          ElMessage.error('处理好友请求失败')
        })
    }

    // 格式化时间
    const formatTime = (time) => {
      if (!time) return ''
      const date = new Date(time)
      return date.toLocaleString()
    }

    // 组件挂载时获取好友请求列表
    onMounted(() => {
      fetchFriendRequests()
    })

    return {
      friendRequests,
      requestLoading,
      fetchFriendRequests,
      handleRequestAction,
      formatTime,
      goBack
    }
  }
}
</script>

<style scoped>
.friend-requests-page {
  padding: 20px;
  background: #fff;
  height: 100%;
  overflow-y: auto;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #eee;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.header-left h2 {
  margin: 0;
  font-size: 20px;
  color: #333;
}

.empty-requests {
  padding: 40px 0;
}

.request-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.request-item {
  display: flex;
  align-items: center;
  padding: 20px;
  background: #f9f9f9;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.request-info {
  flex: 1;
  margin-left: 20px;
}

.request-name {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin-bottom: 5px;
}

.request-tfid {
  font-size: 14px;
  color: #999;
  margin-left: 10px;
}

.request-email {
  font-size: 14px;
  color: #666;
  margin-bottom: 5px;
}

.request-time {
  font-size: 12px;
  color: #999;
}

.request-actions {
  margin-left: 20px;
  display: flex;
  gap: 10px;
}
</style> 